package net.fdukedom.epicurus.presentation.filters;

import net.fdukedom.epicurus.domain.util.HibernateUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Creates and closes session to database.
 *
 * @author Dmitry Y. Kochelaev
 */
public class DomainFilter extends AbstractHttpFilter {


    protected void doFilter(
            HttpServletRequest request,
            HttpServletResponse response,
            FilterChain filterChain
    ) throws IOException, ServletException {
        try {
            HibernateUtil.newSession();
            filterChain.doFilter(request, response);
        } finally {
            HibernateUtil.closeSession();
        }
    }
}
